// pages/statistics/statistics.js
const app = getApp()
import { route } from '../../utils/util'
let that;
Page({
  data: {
    showStat: false,
    daynum: 0, //每月天数
    day: ['周一', '周二', '周三', '周四', '周五', '周六', '周日',],
    tabheight:app.globalData.phone_height
  },
  onLoad: function (options) {
   
  },
  onShow: function () {
    that = this;
    let ui = wx.getStorageSync('userInfo');
    if(!ui){
      return that.setData({showStat:false})
    }
    let year = new Date().getFullYear();
    let month = new Date().getMonth() + 1;
    let date = new Date().getDate(); //几号
    that.setData({ date, year, month, nowMonth: month, nowDate: date,ui })
    that.getCalendar(year, month)

    that.getStatistics()
  },
  // 日历
  getCalendar(year, month) {
    let daynum = new Date(year, month, 0).getDate() // 获取每月多少天
    let weeks = new Date(year, month - 1, 0).getDay()// 获取这个月的1号是周几
    let aNum = weeks == 0 ? 0 : weeks //开始需要空几格
    let bNum = (daynum + aNum) % 7; //最后需要空几格
    bNum == 0 ? bNum : bNum = 7 - bNum
    that.setData({ daynum, aNum, bNum })
  },
  // 切换 上个月 下个月
  updataCalendar(e) {
    // 0:上个月   1:下个月
    let updata = e.currentTarget.dataset.updata;
    let year = this.data.year;
    let month = this.data.month;
    let nowMonth = this.data.nowMonth;
    let date = this.data.date;
    let nowDate = this.data.nowDate;
    switch (updata) {
      case '0':
        month == 1 ? (month = 12, year = year - 1) : month -= 1
        break;
      case '1':
        month == 12 ? (month = 1, year = year + 1) : month += 1
        break;
    }
    date = (nowMonth == month) ? nowDate : 1
    this.getCalendar(year, month)
    this.setData({ year, month, date })
  },
  getStatistics() {
    let ui = this.data.ui;
    if (!ui.openid) {
      route('/pages/mine/mine', {}, 'switchtab')
    } else {
      wx.cloud.callFunction({
        name: 'getrecord',
        data: {
          openid:ui.openid
        },
        success: res => {
          this.setData({showStat:true})
        }, fail: err => {
          console.log(err);
        }
      })
    }
  }
})